package com.angel.utils;

import java.math.BigInteger;
import java.security.MessageDigest;

public class CoderPwd {
    public static final String KEY_SHA = "SHA";
    public static final String KEY_MD5 = "MD5";


    /**
     * MD5加密
     */
    private static byte[] encryptMD5(byte[] data) throws Exception {

        MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
        md5.update(data);

        return md5.digest();

    }

    /**
     * SHA加密
     */
    private static byte[] encryptSHA(byte[] data) throws Exception {
        MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
        sha.update(data);
        return sha.digest();
    }


    /**
     * SHA加密
     * @param pwd 初始秘密
     * @return 返回加密之后的字符串
     */
    public static String encryptingPwdBySha(String pwd) {
        byte[] inputData = pwd.getBytes();
        BigInteger sha = null;
        try {
            sha = new BigInteger(encryptSHA(inputData));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (sha == null) {
            return null;
        }
        return sha.toString(32);
    }

    /**
     * Md5加密
     * @param pwd 初始秘密
     * @return 返回加密之后的字符串
     */
    public static String encryptingPwdByMd5(String pwd) {
        byte[] inputData = pwd.getBytes();
        BigInteger sha = null;
        try {
            sha = new BigInteger(encryptMD5(inputData));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (sha == null) {
            return null;
        }
        return sha.toString(16);
    }

}
